package bank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Login extends JPanel implements ActionListener {
    private Atm frame;
    private Icon btnLand = Util.getIcon("/images/buttonLand.jpg");
    private Icon btnOff = Util.getIcon("/images/buttonOff.jpg");
    private JButton imageBtn1 = new JButton(btnLand);
    private JButton imageBtn2 = new JButton(btnOff);
    private JTextField accountText = new JTextField();
    private JPasswordField password = new JPasswordField();
    private ArrayList<User> users = new ArrayList();
    private int n = 3;


    public Login(Atm frame) {
        this.frame = frame;



        this.setLayout(null);
        this.setOpaque(false);


        JLabel login = new JLabel("账号:");
        login.setFont(new Font("宋体", Font.PLAIN, 30));
        login.setBounds(300, 200, 100, 50);
        this.add(login);
        JLabel pas = new JLabel("密码:");
        pas.setFont(new Font("宋体", Font.PLAIN, 30));
        pas.setBounds(300, 300, 100, 50);
        this.add(pas);


        accountText.setBounds(400, 200, 300, 50);
        this.add(accountText);
        password.setBounds(400, 300, 300, 50);
        this.add(password);


        imageBtn1.setBounds(250, 430, 140, 50);
        this.add(imageBtn1);
        imageBtn1.addActionListener(this);
        imageBtn2.setBounds(650, 430, 140, 50);
        this.add(imageBtn2);
        imageBtn2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        users = frame.getUsers();
        if (e.getSource() == imageBtn1) {



            for (User obj : users) {

                if (accountText.getText().equals(obj.getAccount()) && password.getText().equals(obj.getPsw())) {
                    n=3;
                    accountText.setText("");
                    password.setText("");
                    frame.setIndex(frame.getUsers().indexOf(obj));
                    frame.switchPanel("menu");
                    return;
                }
            }
            n--;
            if (n ==0) {
                JOptionPane.showMessageDialog(this, "输入错误次数过多，再见");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "输入错误请重新输入");
            accountText.setText("");
            password.setText("");



        } else if (e.getSource() == imageBtn2) {
            JOptionPane.showMessageDialog(this, "谢谢使用，再见！");
            System.exit(0);
        }
    }
}
